﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Barebones.Dependencies
{
    public class Dependency<Other> : IDependency
    {
        Action<Other> m_OnAdded;
        Action<Other> m_OnRemoved;

        public Dependency(Action<Other> added)
        {
            m_OnAdded = added;
        }

        public Dependency(Action<Other> added, Action<Other> removed)
            : this(added)
        {
            m_OnRemoved = removed;
        }

        public bool IsFor(Object other)
        {
            return typeof(Other).IsAssignableFrom(other.GetType());
        }

        public void Link(Object other)
        {
            Debug.Assert(IsFor(other));
            m_OnAdded((Other)other);
        }

        public void UnLink(Object other)
        {
            Debug.Assert(IsFor(other));
            if (m_OnRemoved != null)
                m_OnRemoved((Other)other);
        }
    }
}
